#!/usr/bin/env perl
use warnings;
use strict;

use lib 'lib';

use Test::More tests => 8;
use_ok( 'Guile::Space' );

my $scope = {
    foo     => [1 .. 5],
    bar     => [0, [1, 2], 3],
};
my @tests = (
    {   title   => 'simple array access',
        code    => q/ (foo 'get 3) /,
        result  => 4,
    },
    {   title   => 'array in array access',
        code    => q/ ((bar 'get 1) 'get 0) /,
        result  => 1,
    },
    {   title   => 'array size',
        code    => q/ (foo 'size) /,
        result  => 5,
    },
    {   title   => 'last array element index',
        code    => q/ (foo 'last) /,
        result  => 4,
    },
    {   title   => 'set array value',
        code    => q/ (begin (foo 'set (foo 'last) 23) (foo 'get 4)) /,
        result  => 23,
    },
    {   title   => 'creating a new array',
        code    => q/ (begin 
                        (define fnord (make-perl-array)) 
                        (fnord 'set 3 23) 
                        (fnord 'get 3)) /,
        result  => 23,
    },
    {   title   => 'transform perl-array to guile list',
        code    => q/ (list-ref (foo 'to-list) 2) /,
        result  => 3,
    },
);

my $space = Guile::Space->new;

for my $t (@tests) {

  SKIP: {
        if ( my $msg = $t->{skip} ) { skip $msg => 1 }
        my $test = __PACKAGE__->can( $t->{test} || 'is' );
        $test->( $space->run( code => $t->{code}, scope => $scope ),
                 $t->{result},
                 $t->{title},
        );
    }
}



